package org.example.springsecurity_.service.Impl;

import org.example.springsecurity_.domain.MyUserDetails;
import org.example.springsecurity_.domain.User;
import org.example.springsecurity_.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findByName(username);
        if(user==null){
            throw new RuntimeException("用户不存在");
        }

        MyUserDetails myUserDetails = new MyUserDetails();
        myUserDetails.setUser(user);

        return myUserDetails;
    }
}
